using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;

namespace TEngine
{
    public static class UIWindowExtensions
    {
        /// <summary>
        /// 为UIWindow提供CancellationToken的扩展方法
        /// </summary>
        public static CancellationToken GetCancellationTokenOnDestroy(this UIWindow window)
        {
            var proxy = window.gameObject.GetComponent<CancellationProxy>();
            if (proxy == null)
            {
                proxy = window.gameObject.AddComponent<CancellationProxy>();
            }
            return proxy.GetCancellationTokenOnDestroy();
        }

        /// <summary>
        /// 代理MonoBehaviour用于处理取消令牌
        /// </summary>
        private class CancellationProxy : MonoBehaviour
        {
        }
    }
}
